home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / Jims CDEFs 1.50 / demo Source ƒ / utilities / dialogAssist.c next >
Encoding:
Text File  |  1995-10-27  |  28.8 KB  |  1,059 lines  |  [TEXT/KAHL]

  1. //------------------------- © 1994-1995 by James G. Stout --------------------------
  2. //    File        : dialogAssist.c
  3. //    Date        : April 4, 1994
  4. //    Author        : Jim Stout
  5. //    Purpose        : Dialog utilities that make use of ModalDialog a bit easier.
  6. //
  7. //                I got really tired of writing all those GetDItem/SetDItem calls
  8. //                and then having to mix them with Control Manager and Dialog
  9. //                Manager calls (that have various syntax rules). So, these are
  10. //                are a start at some easier routines.
  11. //
  12. //                Also, there are routines here for handling keys, limits on
  13. //                editText DITL items, mouseClicks, cursors and dimming items -
  14. //                and lots of other stuff.  Take a look at the dialogAssist.h
  15. //                file prototypes - the routine names are pretty self explanatory.
  16. //----------------------------------------------------------------------------------
  17.  
  18. #include <GestaltEqu.h>
  19. #include <Traps.h>
  20. #include "dialogAssist.h"
  21. #include "dimText.h"
  22.  
  23. #define BEEP 1
  24.  
  25. #pragma mark _ControlRoutines
  26.  
  27. //----------------------------------------------------------------------------------
  28. //    Function: daToggleCheck
  29. //     Purpose: toggles the state of a checkbox.
  30. //                Does nothing if called for a non-checkbox item.
  31. //
  32. //   returns: void
  33. //----------------------------------------------------------------------------------
  34. void daToggleCheck (DialogPtr d, short i)
  35. {
  36.     short            t;
  37.     ControlHandle    h;
  38.     Rect            r;
  39.  
  40.     GetDItem(d, i, &t, (Handle *)&h, &r);
  41.     if(h && (t & chkCtrl)) {
  42.         SetCtlValue(h, !GetCtlValue(h));
  43.     }
  44. }
  45.  
  46. //----------------------------------------------------------------------------------
  47. //    Function: daToggleRadio
  48. //     Purpose: set radio button "i" ON and turns the others off.
  49. //                N.B. This routine assumes that radio buttons are
  50. //                sequentially numbered from "first" to "last" 
  51. //                Does nothing if called for a non-radioBtn item.
  52. //
  53. //   returns: void
  54. //----------------------------------------------------------------------------------
  55. void daToggleRadio (DialogPtr d, short i, short first, short last)
  56. {
  57.     short            t,inx;
  58.     ControlHandle    h;
  59.     Rect            r;
  60.  
  61.     for(inx=first;inx<=last;inx++) {
  62.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  63.         if(h && (t & radCtrl)) {
  64.             if(inx == i)
  65.                 SetCtlValue(h, ON);
  66.             else
  67.                 SetCtlValue(h, OFF);
  68.         }
  69.     }
  70. }
  71.  
  72. //----------------------------------------------------------------------------------
  73. //    Function: daGetRadio
  74. //     Purpose: Return the number of the "on" radio button in a set.
  75. //                N.B. This routine assumes that radio buttons are
  76. //                sequentially numbered from "first" to "last" 
  77. //                Does nothing if called for a non-radioBtn item.
  78. //
  79. //   returns: void
  80. //----------------------------------------------------------------------------------
  81. short daGetRadio (DialogPtr d, short first, short last)
  82. {
  83.     short            t,inx;
  84.     ControlHandle    h;
  85.     Rect            r;
  86.  
  87.     for(inx=first;inx<=last;inx++) {
  88.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  89.         if(h && (t & radCtrl)) {
  90.             if(GetCtlValue(h))
  91.                 return(inx);
  92.         }
  93.     }
  94.     return(0);
  95. }
  96. //----------------------------------------------------------------------------------
  97. //    Function: daToggleCtl
  98. //     Purpose: toggles the state of a control.
  99. //                Does nothing if called for a non-control item.
  100. //
  101. //   returns: void
  102. //----------------------------------------------------------------------------------
  103. void daToggleCtl (DialogPtr d, short i)
  104. {
  105.     short            t;
  106.     ControlHandle    h;
  107.     Rect            r;
  108.  
  109.     GetDItem(d, i, &t, (Handle *)&h, &r);
  110.     if(h && (t & ctrlItem)) {
  111.         SetCtlValue(h, !GetCtlValue(h));
  112.     }
  113. }
  114. //----------------------------------------------------------------------------------
  115. //    Function: daGetCtlHandle
  116. //     Purpose: get the handle to a control item in a dialog.
  117. //                Does nothing if called for a non-control item.
  118. //
  119. //   returns: value of control
  120. //----------------------------------------------------------------------------------
  121. ControlHandle daGetCtlHandle (DialogPtr d, short i)
  122. {
  123.     short            t;
  124.     ControlHandle    h;
  125.     Rect            r;
  126.  
  127.     GetDItem(d, i, &t, (Handle *)&h, &r);
  128.     if(h && (t & ctrlItem)) {
  129.         return (h);
  130.     }
  131.     return(0);
  132. }
  133.  
  134. //----------------------------------------------------------------------------------
  135. //    Function: daGetCtlMax
  136. //     Purpose: get the maximum value of a control item in a dialog.
  137. //                Does nothing if called for a non-control item.
  138. //
  139. //   returns: value of control
  140. //----------------------------------------------------------------------------------
  141. short daGetCtlMax (DialogPtr d, short i)
  142. {
  143.     short            t;
  144.     ControlHandle    h;
  145.     Rect            r;
  146.  
  147.     GetDItem(d, i, &t, (Handle *)&h, &r);
  148.     if(h && (t & ctrlItem)) {
  149.         return ((**h).contrlMax);
  150.     }
  151.     return(0);
  152. }
  153. //----------------------------------------------------------------------------------
  154. //    Function: daGetCtlMin
  155. //     Purpose: get the minimum value of a control item in a dialog.
  156. //                Does nothing if called for a non-control item.
  157. //
  158. //   returns: value of control
  159. //----------------------------------------------------------------------------------
  160. short daGetCtlMin (DialogPtr d, short i)
  161. {
  162.     short            t;
  163.     ControlHandle    h;
  164.     Rect            r;
  165.  
  166.     GetDItem(d, i, &t, (Handle *)&h, &r);
  167.     if(h && (t & ctrlItem)) {
  168.         return ((**h).contrlMin);
  169.     }
  170.     return(0);
  171. }
  172.  
  173. //----------------------------------------------------------------------------------
  174. //    Function: daGetCtlValue
  175. //     Purpose: get the value of a control item in a dialog.
  176. //                Does nothing if called for a non-control item.
  177. //
  178. //   returns: value of control
  179. //----------------------------------------------------------------------------------
  180. short daGetCtlValue (DialogPtr d, short i)
  181. {
  182.     short            t;
  183.     ControlHandle    h;
  184.     Rect            r;
  185.  
  186.     GetDItem(d, i, &t, (Handle *)&h, &r);
  187.     if(h && (t & ctrlItem)) {
  188.         return (GetCtlValue(h));
  189.     }
  190.     return(0);
  191. }
  192. //----------------------------------------------------------------------------------
  193. //    Function: daSetCtlValue
  194. //     Purpose: set the value of a control item in a dialog.
  195. //                Does nothing if called for a non-control item.
  196. //
  197. //   returns: void
  198. //----------------------------------------------------------------------------------
  199.  
  200. void daSetCtlValue (DialogPtr d, short i, short newVal)
  201. {
  202.     short            t;
  203.     ControlHandle    h;
  204.     Rect            r;
  205.  
  206.     GetDItem(d, i, &t, (Handle *)&h, &r);
  207.     if(h && (t & ctrlItem)) {
  208.         SetCtlValue(h, newVal);
  209.     }
  210. }
  211.  
  212. //----------------------------------------------------------------------------------
  213. //    Function: daGetCtlTitle
  214. //     Purpose: Get the title of a control item in a dialog.
  215. //                Does nothing if called for a non-control item.
  216. //
  217. //   returns: void
  218. //----------------------------------------------------------------------------------
  219. void daGetCtlTitle (DialogPtr d, short i, Str255 theTitle)
  220. {
  221.     short            t;
  222.     ControlHandle    h;
  223.     Rect            r;
  224.  
  225.     GetDItem(d, i, &t, (Handle *)&h, &r);
  226.     if(h && (t & ctrlItem)) {
  227.         pStrCopy((char *)theTitle,(char *)(*h)->contrlTitle);
  228.     }
  229. }
  230. //----------------------------------------------------------------------------------
  231. //    Function: daSetCtlTitle
  232. //     Purpose: set the title of a control item in a dialog.
  233. //                Does nothing if called for a non-control item.
  234. //
  235. //   returns: void
  236. //----------------------------------------------------------------------------------
  237. void daSetCtlTitle (DialogPtr d, short i, Str255 newTitle)
  238. {
  239.     short            t;
  240.     ControlHandle    h;
  241.     Rect            r;
  242.  
  243.     GetDItem(d, i, &t, (Handle *)&h, &r);
  244.     if(h && (t & ctrlItem)) {
  245.         SetCTitle(h, newTitle);
  246.     }
  247. }
  248.  
  249. //----------------------------------------------------------------------------------
  250. //    Function: daGetCtlRefCon
  251. //     Purpose: get the refCon of a control item in a dialog.
  252. //                Does nothing if called for a non-control item.
  253. //
  254. //   returns: refCon
  255. //----------------------------------------------------------------------------------
  256. long daGetCtlRefCon (DialogPtr d, short i)
  257. {
  258.     short            t;
  259.     ControlHandle    h;
  260.     Rect            r;
  261.  
  262.     GetDItem(d, i, &t, (Handle *)&h, &r);
  263.     if(h && (t & ctrlItem))
  264.         return(GetCRefCon(h));
  265.     else
  266.         return(0L);
  267. }
  268.  
  269. //----------------------------------------------------------------------------------
  270. //    Function: daSetCtlRefCon
  271. //     Purpose: set the refCon of a control item in a dialog.
  272. //                Does nothing if called for a non-control item.
  273. //
  274. //----------------------------------------------------------------------------------
  275. void daSetCtlRefCon(DialogPtr d, short i,long theValue)
  276. {    
  277.     short            t;
  278.     ControlHandle    h;
  279.     Rect            r;
  280.  
  281.     GetDItem(d, i, &t, (Handle *)&h, &r);
  282.     if(h && (t & ctrlItem))
  283.         SetCRefCon(h,theValue);
  284. }
  285.  
  286. //----------------------------------------------------------------------------------
  287. //    Function: daGetCtlRect
  288. //     Purpose: get the rect of a control item in a dialog.
  289. //                Does nothing if called for a non-control item.
  290. //
  291. //----------------------------------------------------------------------------------
  292. void daGetCtlRect(DialogPtr d, short i, Rect * r)
  293. {
  294.     short            t;
  295.     ControlHandle    h;
  296.     Rect            r1;
  297.     
  298.     GetDItem(d, i, &t, (Handle *)&h, &r1);
  299.     if(h && (t & ctrlItem))
  300.         *r = (*h)->contrlRect;
  301. }
  302.  
  303. //----------------------------------------------------------------------------------
  304. //    Function: daSetCtlRect
  305. //     Purpose: set the rect of a control item in a dialog.
  306. //                Does nothing if called for a non-control item.
  307. //
  308. //----------------------------------------------------------------------------------
  309. void daSetCtlRect(DialogPtr d, short i, Rect * newRect)
  310. {
  311.     short            t,h,v;
  312.     Rect            r;
  313.     ControlHandle    theCtl;
  314.     
  315.     GetDItem(d, i, &t, (Handle *)&theCtl, &r);
  316.     if(theCtl && (t & ctrlItem)) {
  317.         h = (*newRect).right - (*newRect).left;
  318.         v = (*newRect).bottom - (*newRect).top;
  319.         SizeControl(theCtl, h, v);
  320.     }
  321. }
  322.  
  323. //----------------------------------------------------------------------------------
  324. //    Function: daIsHidden
  325. //     Purpose: find out if a dialog item is hidden.
  326. //            :
  327. //
  328. //   returns: true if item is hidden
  329. //----------------------------------------------------------------------------------
  330. Boolean daIsHidden(DialogPtr d, short i)
  331. {
  332.     short            t;
  333.     ControlHandle    h;
  334.     Rect            r;
  335.  
  336.     GetDItem(d, i, &t, (Handle *)&h, &r);
  337.     if(r.left > 8192)
  338.         return(true);
  339.     return(false);
  340. }
  341. #pragma mark _MouseRoutines
  342.  
  343. //----------------------------------------------------------------------------------
  344. //    Function: daMouseItem
  345. //     Purpose: check entire DITL list for a mouse click in an item.
  346. //            :
  347. //   returns: item clicked
  348. //----------------------------------------------------------------------------------
  349. short daMouseWhere (DialogPtr d, EventRecord * theEvt)
  350. {
  351.     short            inx,max,t;
  352.     ControlHandle    h;
  353.     Rect            r;
  354.     Point            mp;
  355.     
  356.     mp = theEvt->where;
  357.     GlobalToLocal(&mp);
  358.  
  359.     max = (**(short **) (((DialogPeek) d)->items)) +1;
  360.     for(inx=1;inx<=max;inx++) {
  361.         GetDItem(d, inx, &t, (Handle *)&h, &r);
  362.         if(PtInRect(mp, &r))
  363.             return(inx);
  364.     }
  365.     return(0);
  366. }
  367. //----------------------------------------------------------------------------------
  368. //    Function: daMouseInItem
  369. //     Purpose: check a single DITL item for a mouse click in that item.
  370. //
  371. //   returns: true if item clicked, false otherwise.
  372. //----------------------------------------------------------------------------------
  373. Boolean daMouseInItem (DialogPtr d, short i, EventRecord * theEvt)
  374. {
  375.     short            t;
  376.     ControlHandle    h;
  377.     Rect            r;
  378.     Point            mp;
  379.     
  380.     mp = theEvt->where;
  381.     GlobalToLocal(&mp);
  382.  
  383.     GetDItem(d, i, &t, (Handle *)&h, &r);
  384.     if(PtInRect(mp, &r))
  385.         return(true);
  386.     return(false);
  387. }
  388. //----------------------------------------------------------------------------------
  389. //    Function: daEditCursor
  390. //     Purpose: set IBeam cursor if mouse if over the current editText item.
  391. //                typically called from top of dialog filterProc
  392. //   returns: void
  393. //----------------------------------------------------------------------------------
  394. void daEditCursor (DialogPtr d)
  395. {
  396.     short            i,t;
  397.     ControlHandle    h;
  398.     Rect            r;
  399.     Point            mp;
  400.     
  401.     i = ((DialogPeek)d)->editField + 1;
  402.     GetMouse(&mp);
  403.     GetDItem(d, i, &t, (Handle *)&h, &r);
  404.     if(PtInRect(mp, &r))
  405.         SetCursor(*(GetCursor(1)));
  406.     else
  407.         InitCursor();
  408. }
  409. #pragma mark _TextRoutines
  410. //----------------------------------------------------------------------------------
  411. //    Function: daSetIText
  412. //     Purpose: set text into a DITL item.
  413. //                Does nothing if called for non-editText or non-statText item.
  414. //   returns: void.
  415. //----------------------------------------------------------------------------------
  416. void daSetIText (DialogPtr d, short i, Str255 newText)
  417. {
  418.     short            t;
  419.     Handle            h;
  420.     Rect            r;
  421.  
  422.     GetDItem(d, i, &t, &h, &r);
  423.     if(h && (t & statText || t & editText)) {
  424.         SetIText(h, newText);
  425.     }
  426. }
  427. //----------------------------------------------------------------------------------
  428. //    Function: daGetIText
  429. //     Purpose: get the text from a DITL item.
  430. //                Does nothing if called for non-editText or non-statText item.
  431. //   returns: void.
  432. //----------------------------------------------------------------------------------
  433. void daGetIText (DialogPtr d, short i, Str255 s)
  434. {
  435.     short            t;
  436.     Handle            h;
  437.     Rect            r;
  438.  
  439.     s[0] = 0;
  440.     GetDItem(d, i, &t, &h, &r);
  441.     if(h && (t & statText || t & editText)) {
  442.         GetIText(h, s);
  443.     }
  444. }
  445.  
  446. //----------------------------------------------------------------------------------
  447. //    Function: daSetINum
  448. //     Purpose: set a number into a DITL item.
  449. //                Does nothing if called for non-editText or non-statText item.
  450. //   returns: void.
  451. //----------------------------------------------------------------------------------
  452. void daSetINum (DialogPtr d, short i, long newNum)
  453. {
  454.     short            t;
  455.     Handle            h;
  456.     Rect            r;
  457.     Str255            s;
  458.  
  459.     GetDItem(d, i, &t, &h, &r);
  460.     if(h && (t & statText || t & editText)) {
  461.         NumToString(newNum, s);
  462.         SetIText(h, s);
  463.     }
  464. }
  465. //----------------------------------------------------------------------------------
  466. //    Function: daGetINum
  467. //     Purpose: get the contents a DITL item as a number.
  468. //                Does nothing if called for non-editText or non-statText item.
  469. //   returns: contents of text item as a long.
  470. //----------------------------------------------------------------------------------
  471. long daGetINum (DialogPtr d, short i)
  472. {
  473.     short            t;
  474.     Handle            h;
  475.     Rect            r;
  476.     Str255            s;
  477.     long            l=0L;
  478.  
  479.     GetDItem(d, i, &t, &h, &r);
  480.     if(h && (t & statText || t & editText)) {
  481.         GetIText(h, s);
  482.         StringToNum(s, &l);
  483.     }
  484.     return(l);
  485. }
  486. //----------------------------------------------------------------------------------
  487. //    Function: daSelIText
  488. //     Purpose: select the text of a DITL editText item.
  489. //                Does nothing if called for a non-editText item.
  490. //   returns: void.
  491. //----------------------------------------------------------------------------------
  492. void daSelIText(DialogPtr d, short i)
  493. {
  494.     short            t;
  495.     Handle            h;
  496.     Rect            r;
  497.  
  498.     GetDItem(d, i, &t, &h, &r);
  499.     if(h && (t & editText)) {
  500.         SelIText(d, i, 0,32767);
  501.     }
  502.  
  503. }
  504.  
  505. //----------------------------------------------------------------------------------
  506. //    Function: daSetInsert
  507. //     Purpose: set the insertion point for a DITL editText item before character
  508. //                "before".  If "before" is zero, set insertion point at end.
  509. //                Does nothing if called for a non-editText item.
  510. //   returns: void.
  511. //----------------------------------------------------------------------------------
  512. void daSetInsert(DialogPtr d, short i, short before)
  513. {
  514.     short            t;
  515.     Handle            h;
  516.     Rect            r;
  517.  
  518.     GetDItem(d, i, &t, &h, &r);
  519.     if(h && (t & editText)) {
  520.         if(before)
  521.             SelIText(d, i, before-1, before-1);
  522.         else
  523.             SelIText(d, i, 32767,32767);
  524.     }
  525.  
  526. }
  527. #pragma mark _DefaultButton
  528.  
  529. //----------------------------------------------------------------------------------
  530. //    Function: daSetDefItem
  531. //     Purpose: set default item (button) for a dialog and redraw it.
  532. //
  533. //   returns: void.
  534. //----------------------------------------------------------------------------------
  535. void daSetDefItem(DialogPtr d, short defItem)
  536. {
  537.     short            t;
  538.     Handle            h;
  539.     Rect            r;
  540.     PenState        ps;
  541.     long            cQD;
  542.     RGBColor        fg,bg;
  543.     
  544.     GetDItem(d, defItem, &t, &h, &r);
  545.     if(h && (t == 4)) {
  546.         if(((DialogPeek)d)->aDefItem != defItem) {
  547.             GetPenState(&ps);
  548.             cQD = daGestalt(gestaltQuickdrawVersion);
  549.             if(cQD >= gestalt8BitQD) {
  550.                 GetForeColor(&fg);
  551.                 GetBackColor(&bg);
  552.                 RGBForeColor(&bg);
  553.             }
  554.             else
  555.                 PenMode(srcXor);
  556.             daDrawDefault(d);
  557.             if(cQD >= gestalt8BitQD) 
  558.                 RGBForeColor(&fg);
  559.             SetPenState(&ps);
  560.             ((DialogPeek)d)->aDefItem = defItem;
  561.             daDrawDefault(d);
  562.         }
  563.     }
  564. }
  565.  
  566. //----------------------------------------------------------------------------------
  567. //    Function: daDrawDefault
  568. //     Purpose: draws the default button outline.
  569. //                Should be called from filterProc in response to udpdateEvt
  570. //   returns: void.
  571. //----------------------------------------------------------------------------------
  572. void daDrawDefault (DialogPtr d)
  573. {
  574.     short            defItem,t;
  575.     Handle            h;
  576.     Rect            r;
  577.     PenState        ps;
  578.  
  579.     defItem = ((DialogPeek)d)->aDefItem;
  580.     GetDItem(d, defItem, &t, &h, &r);
  581.     if(h && (t == 4)) {
  582.         GetPenState(&ps);
  583.         PenSize(3,3);
  584.         InsetRect(&r, -4,-4);
  585.         FrameRoundRect(&r, 16, 16);
  586.         SetPenState(&ps);
  587.     }
  588.     
  589. }
  590.  
  591. #pragma mark _KeyRoutines
  592.  
  593. //----------------------------------------------------------------------------------
  594. //    Function: daShiftSelect
  595. //     Purpose: handle shift arrow key selection.
  596. //            : call from your filter routine if the shiftkey is down and the
  597. //            : right or left arrow keys are pressed.
  598. //----------------------------------------------------------------------------------
  599. Boolean daShiftSelect(DialogPtr d, char key)
  600. {        
  601.     short start, end;
  602.     
  603.     if(key != _LEFT && key != _RIGHT)
  604.         return false;
  605.         
  606.     start = (**(*(DialogPeek)d).textH).selStart;
  607.     end = (**(*(DialogPeek)d).textH).selEnd;
  608.     if(key == _LEFT)
  609.         start--;
  610.     else
  611.     if(key == _RIGHT)
  612.         end++;
  613.     if(start >= 0)
  614.         TESetSelect(start, end,    (*(DialogPeek)d).textH);
  615.     return true;
  616. }
  617. //----------------------------------------------------------------------------------
  618. //    Function: daOptionDown
  619. //     Purpose: check for optionKey press without having an eventRecord.
  620. //
  621. //   returns: true if option key is pressed.
  622. //----------------------------------------------------------------------------------
  623. Boolean daOptionDown()
  624. {
  625.     KeyMap    theKeys;
  626.  
  627.     GetKeys(theKeys);
  628.     if(BitTst(&theKeys[1],29L))            // option key down
  629.         return(true);
  630.     return(false);
  631. }
  632. //----------------------------------------------------------------------------------
  633. //    Function: daCmdDown
  634. //     Purpose: check for commandKey press without having an eventRecord.
  635. //
  636. //   returns: true if command key is pressed.
  637. //----------------------------------------------------------------------------------
  638. Boolean daCmdDown()
  639. {
  640.     KeyMap    theKeys;
  641.  
  642.     GetKeys(theKeys);
  643.     if(BitTst(&theKeys[1],16L))            // command key down
  644.         return(true);
  645.     return(false);
  646. }
  647. //----------------------------------------------------------------------------------
  648. //    Function: daCntlDown
  649. //     Purpose: check for controlKey press without having an eventRecord.
  650. //
  651. //   returns: true if control key is pressed.
  652. //----------------------------------------------------------------------------------
  653. Boolean daCntlDown()
  654. {
  655.     KeyMap    theKeys;
  656.  
  657.     GetKeys(theKeys);
  658.     if(BitTst(&theKeys[1],28L))            // control key down
  659.         return(true);
  660.     return(false);
  661. }
  662.  
  663. //----------------------------------------------------------------------------------
  664. //    Function: daExitKey
  665. //     Purpose: check for a key press that should exit the dialog.
  666. //                Flashes default button (OK) if RETURN  or ENTER
  667. //                Flashes cancel button if ESCAPE or cmd-period.
  668. //
  669. //   returns: true if dialog should be closed.
  670. //----------------------------------------------------------------------------------
  671. Boolean daExitKey(DialogPtr    d, EventRecord *evt, short *i, short cancelItem)
  672. {
  673.     short            t,defItem;
  674.     char            key;
  675.     Handle            h;
  676.     Rect            r;
  677.     long            ticks;
  678.     Boolean            result = false;
  679.  
  680.     defItem = ((DialogPeek)d)->aDefItem;
  681.     if(!defItem)
  682.         return false;
  683.     key = (evt->message & charCodeMask);
  684.     
  685.      if (key == _RETURNKEY || key == _ENTRKEY){
  686.         GetDItem(d, defItem, &t, &h, &r);
  687.          *i = defItem;
  688.         result = true;
  689.     }
  690.     else
  691.      if (key == _ESCAPEKEY || (key == _PERIODKEY && daCmdDown()) ) {
  692.         GetDItem(d, cancelItem, &t, &h, &r);
  693.          *i = cancelItem;
  694.         result = true;
  695.     }
  696.     if(h && result) {
  697.         HiliteControl((ControlHandle)h, inButton);
  698.         Delay(8L,&ticks);
  699.         HiliteControl((ControlHandle)h, 0);
  700.     }
  701.     return(result);
  702. }
  703. //----------------------------------------------------------------------------------
  704. //    Function: daEnterNumber
  705. //     Purpose: restricts editText item to numeric input only.
  706. //
  707. //   returns: false if keystroke should be processed by ModalDialog.
  708. //----------------------------------------------------------------------------------
  709. Boolean daEnterNumber(DialogPtr d, short i, long min, long max, char key)
  710. {
  711.     Str255    s;
  712.     short    len;
  713.     long    num;
  714.     
  715.     if(daTEkey(key) || key == _PLUS || key == _MINUS)
  716.         return(false);
  717.     if(key < _ZERO || key > _NINE) {
  718. #ifdef BEEP
  719.         SysBeep(1);
  720. #endif
  721.         return(true);
  722.     }
  723.     
  724.     DlgCut(d);
  725.     daGetIText(d, i, s);
  726.     len = s[0];
  727.     s[++len] = key;
  728.     s[0] = len;
  729.     StringToNum(s, &num);
  730.     if(num > max || num < min) {
  731. #ifdef BEEP
  732.         DlgPaste(d);
  733.         SysBeep(1);
  734. #endif
  735.         return(true);
  736.     }
  737.     return(false);
  738. }
  739. //----------------------------------------------------------------------------------
  740. //    Function: daEnterPassword
  741. //     Purpose: restricts editText item to numeric input only.
  742. //
  743. //   returns: false if keystroke should be processed by ModalDialog.
  744. //----------------------------------------------------------------------------------
  745. Boolean daEnterPassword(DialogPtr d, short i, short len, char key, Str255 pw)
  746. {
  747.     Str255    s;
  748.     short    inx;
  749.     
  750.     if(key == _BACK) {                            // allow backspace to delete
  751.         daSetIText(d, i, "\p");
  752.         daGetIText(d, i, pw);
  753.         return(true);
  754.     }
  755.     if(key == _TAB)                                // let TAB work
  756.         return(false);
  757.         
  758.     if(daTEkey(key) || key == _DELKEY) {        // disallow edit keys
  759. #ifdef BEEP
  760.         SysBeep(1);
  761. #endif
  762.         return(true);
  763.     }
  764.         
  765.     if((**(*(DialogPeek)d).textH).selStart !=    // delete selection
  766.         (**(*(DialogPeek)d).textH).selEnd) {
  767.         TEDelete((*(DialogPeek)d).textH);
  768.         daSetIText(d, i, "\p");
  769.         daGetIText(d, i, pw);
  770.     }
  771.     daGetIText(d, i, s);
  772.     if(s[0] < len) {
  773.         TEKey('•',(*(DialogPeek)d).textH);        // insert bullet
  774.         inx = pw[0];                            // now save real char in
  775.         pw[++inx] = key;                        // the password buffer
  776.         pw[0] = inx;
  777.     }
  778.     else
  779. #ifdef BEEP
  780.         SysBeep(1);
  781. #endif
  782.     return(true);                                // we did it, ModalDialog
  783. }
  784. //----------------------------------------------------------------------------------
  785. //    Function: daLimitText
  786. //     Purpose: restricts editText item to "count" number of characters.
  787. //
  788. //   returns: false if keystroke should be processed by ModalDialog.
  789. //----------------------------------------------------------------------------------
  790. Boolean daLimitText(DialogPtr d, short i, short count, char key)
  791. {
  792.     Str255    s;
  793.     
  794.     if(daTEkey(key))
  795.         return(false);
  796.         
  797.     daGetIText(d, i, s);
  798.     if(s[0] >= count) {
  799. #ifdef BEEP
  800.         SysBeep(1);
  801. #endif
  802.         return(true);
  803.     }
  804.     else
  805.         return(false);
  806. }
  807. //----------------------------------------------------------------------------------
  808. //    Function: daTEkey
  809. //     Purpose: check for "edit" type keystroke.
  810. //
  811. //   returns: true if key is an "edit" key.
  812. //----------------------------------------------------------------------------------
  813. Boolean daTEkey(char key)
  814. {
  815.     if(key == _UP || key == _DOWN || key == _RIGHT || key == _LEFT ||
  816.         key == _PAGEUP || key == _PAGEDOWN || key == _BACK || key == _TAB)
  817.         return(true);
  818.     return(false);
  819. }
  820. //----------------------------------------------------------------------------------
  821. //    Function: daForwardDelete
  822. //     Purpose: process a forward delete key in an editText item.
  823. //
  824. //   returns: void
  825. //----------------------------------------------------------------------------------
  826. Boolean daForwardDel(DialogPtr d, char key)
  827. {
  828.     if(key != _DELKEY)
  829.         return(false);
  830.         
  831.     if((**(*(DialogPeek)d).textH).selStart ==
  832.         (**(*(DialogPeek)d).textH).selEnd) {
  833.         TESetSelect((**(*(DialogPeek)d).textH).selStart,
  834.                     (**(*(DialogPeek)d).textH).selEnd+1,
  835.                     (*(DialogPeek)d).textH);
  836.     }
  837.     TEDelete((*(DialogPeek)d).textH);
  838.     return(true);
  839. }
  840. #pragma mark _DrawRoutines
  841.  
  842. //----------------------------------------------------------------------------------
  843. //    Function: daSetIDraw
  844. //     Purpose: set a userItem drawProc.
  845. //
  846. //   returns: void
  847. //----------------------------------------------------------------------------------
  848. void daSetIDraw (DialogPtr d, short i, ProcPtr drawIt)
  849. {
  850.     short            t;
  851.     Handle            h;
  852.     Rect            r;
  853.  
  854.     GetDItem(d, i, &t, &h, &r);
  855.     if(t == userItem || t == itemDisable) {
  856.         SetDItem(d, i, t, (Handle)drawIt, &r);
  857.     }
  858. }
  859.  
  860. //----------------------------------------------------------------------------------
  861. //    Function: daDrawIFrame
  862. //     Purpose: frame a userItem.
  863. //
  864. //   returns: void
  865. //----------------------------------------------------------------------------------
  866. pascal void daDrawIFrame(DialogPtr d, short i)
  867. {
  868.     short            t;
  869.     Handle            h;
  870.     Rect            r;
  871.  
  872.     GetDItem(d, i, &t, &h, &r);
  873.     FrameRect(&r);
  874. }
  875.  
  876. #pragma mark _ItemDimming
  877. //----------------------------------------------------------------------------------
  878. //    Function: daGetDim
  879. //     Purpose: return state of DITL item.
  880. //
  881. //   returns: true if dimmed (inactive control or disabled item)
  882. //----------------------------------------------------------------------------------
  883. Boolean    daGetDim (DialogPtr d, short i)
  884. {
  885.     short            t;
  886.     Handle            h;
  887.     Rect            r;
  888.  
  889.     GetDItem(d, i, &t, &h, &r);
  890.     if(h) {
  891.         if(t & ctrlItem) {
  892.             return(!(*(ControlHandle)h)->contrlHilite);    
  893.         }
  894.         else
  895.             return(t & itemDisable);
  896.     }
  897.     return(false);
  898. }
  899. //----------------------------------------------------------------------------------
  900. //    Function: daDimItems
  901. //     Purpose: dim a series of DITL items from first to last (must be sequential) 
  902. //                and draws them in gray.
  903. //
  904. //   returns: void
  905. //----------------------------------------------------------------------------------
  906. void daDimItems (DialogPtr d, short first, short last, Boolean dim)
  907. {
  908.     short            inx;
  909.  
  910.     for(inx=first;inx<=last;inx++)
  911.         daDimOne(d, inx, dim);
  912. }
  913. //----------------------------------------------------------------------------------
  914. //    Function: daDimOne
  915. //     Purpose: dim (disable or inactivate) a single item.
  916. //
  917. //   returns: void
  918. //----------------------------------------------------------------------------------
  919. void daDimOne (DialogPtr d, short i, Boolean dim)
  920. {
  921.     short            t;
  922.     ControlHandle    h;
  923.     Rect            r;
  924.  
  925.     GetDItem(d, i, &t, (Handle *)&h, &r);
  926.     if(h) {    
  927.         if(t & ctrlItem) {
  928.             if(dim)
  929.                 HiliteControl(h, DIM);
  930.             else
  931.                 HiliteControl(h, NOTDIM);
  932.         }
  933.         else
  934.         if((t & statText) || (t & editText))
  935.              dimText(d, i, dim);                // in dimText.c
  936.     }
  937. }
  938.  
  939. #pragma mark _UtilityRoutines
  940. //----------------------------------------------------------------------------------
  941. //    Function: daGestalt
  942. //     Purpose: call Gestalt if available.
  943. //
  944. //   returns: Gestalt response
  945. //----------------------------------------------------------------------------------
  946. long daGestalt (OSType selector)
  947. {
  948.     OSErr        err;
  949.     long        gResult;
  950.     
  951.     if(trapIsAvailable(_Gestalt)) {
  952.         err = Gestalt(selector,&gResult);
  953.         if(err == noErr)
  954.             return(gResult);
  955.     }
  956.     return(-1L);
  957. }
  958.  
  959. //----------------------------------------------------------------------------------
  960. //    Function: daHasFeature
  961. //     Purpose: call Gestalt to determine a given feature.
  962. //
  963. //   returns: true if feature present
  964. //----------------------------------------------------------------------------------
  965. Boolean daHasFeature(OSType selector, short bitToCheck)
  966. {    
  967.     OSErr        err;
  968.     long        gResult;
  969.     Boolean        result = false;
  970.     
  971.     if(trapIsAvailable(_Gestalt)) {
  972.         err = Gestalt(selector,&gResult);
  973.         if(err == noErr) {
  974.             if(BitTst(&gResult, 31-bitToCheck))
  975.                 result = true;
  976.         }
  977.     }
  978.     return(result);
  979. }
  980. //----------------------------------------------------------------------------------
  981. //    Generic routine to see if a given trap is available
  982. //----------------------------------------------------------------------------------
  983. extern Boolean    trapIsAvailable(short theTrap)
  984. {
  985.     TrapType    tType;
  986.     
  987.     tType = getTrapType(theTrap);
  988.     
  989.     if(tType == ToolTrap) {
  990.         theTrap &= 0x07ff;
  991.         if(theTrap >= numToolBoxTraps())
  992.             theTrap = _Unimplemented;
  993.     }
  994.     return(( NGetTrapAddress(theTrap, tType) !=
  995.         NGetTrapAddress(_Unimplemented, ToolTrap) ));
  996. }
  997. //----------------------------------------------------------------------------------
  998. //    Needed for "trapIsAvailable" routine
  999. //----------------------------------------------------------------------------------
  1000. static TrapType getTrapType(short theTrap)
  1001. {
  1002.     if((theTrap &= 0x0800))
  1003.         return(ToolTrap);
  1004.     return(OSTrap);
  1005. }
  1006. //----------------------------------------------------------------------------------
  1007. //    Needed for "trapIsAvailable" routine
  1008. //----------------------------------------------------------------------------------
  1009. static short numToolBoxTraps()
  1010. {
  1011.     if(NGetTrapAddress(_InitGraf, ToolTrap) ==
  1012.         NGetTrapAddress(0xaa6e, ToolTrap))
  1013.         return(0x0200);
  1014.     return(0x0400);
  1015. }
  1016. //----------------------------------------------------------------------------------
  1017. //    Concatenates pascal strings - adds p2 to p1
  1018. //----------------------------------------------------------------------------------
  1019. extern void pStrCat(char * p1, char * p2)
  1020. {
  1021.     register int    i,j,k=0;
  1022.  
  1023.     i = p1[0];
  1024.     j = p2[0];
  1025.     p1[0] = i+j;
  1026.     
  1027.     while (++k<=j) p1[++i] = p2[k];
  1028. }
  1029.  
  1030. //----------------------------------------------------------------------------------
  1031. //    Copies pascal string - source to dest
  1032. //----------------------------------------------------------------------------------
  1033. extern void pStrCopy(char * source, char * dest) 
  1034. {
  1035.     short len;
  1036.     
  1037.     len = *dest++ = *source++;
  1038.     while (--len>=0)
  1039.         *dest++ = *source++;
  1040. }
  1041. extern void centerDialog(DialogPtr d)
  1042. {
  1043.     short        windW,windH;
  1044.     short        rectW,rectH;
  1045.     Rect        wRect;
  1046.     
  1047.     wRect = d->portRect;
  1048.     
  1049.     windW = wRect.right-wRect.left;
  1050.     windH = wRect.bottom-wRect.top;
  1051.     
  1052.     rectW = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
  1053.     rectH = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  1054.     
  1055.     MoveWindow    (d,
  1056.                 qd.screenBits.bounds.left+(rectW-windW)/2,
  1057.                 qd.screenBits.bounds.top+(rectH-windH)/3,
  1058.                 false);
  1059. }